﻿
jQuery.extend(jQuery.validator.defaults, { errorElement: 'span',
    errorPlacement: function (error, element) {
        element.parent().append(error);
        //if (element.attr("id") == "captcha") {
        //   error.insertAfter(".captcha-img-refresh");
        //} else {
        //element.parent().append(error);
        //   error.insertAfter(element);
        //}
    } /*, invalidHandler: $.watermark.showAll*/
});

jQuery.extend(jQuery.validator.messages, {
    required: "不可为空！",
    remote: "请修正该字段",
    email: "请输入正确格式的电子邮件！",
    url: "请输入合法的网址！",
    date: "请输入合法的日期！",
    dateISO: "请输入合法的日期 (ISO)！",
    number: "请输入合法的数字！",
    digits: "只能输入整数！",
    creditcard: "请输入合法的信用卡号！",
    equalTo: "请再次输入相同的值！",
    accept: "请输入拥有合法后缀名的字符串",
    maxlength: jQuery.validator.format("请输入一个 长度最多是 {0} 的字符串！"),
    minlength: jQuery.validator.format("请输入一个 长度最少是 {0} 的字符串！"),
    rangelength: jQuery.validator.format("请输入 一个长度介于 {0} 和 {1} 之间的字符串！"),
    range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值！"),
    max: jQuery.validator.format("请输入一个最大为{0} 的值！"),
    min: jQuery.validator.format("请输入一个最小为{0} 的值！")
});
(function ($) {
    $.validator.addMethod("alphanumeric", function (value, element) {
        return this.optional(element) || /^\w+$/i.test(value);
    }, "Letters, numbers or underscores only please");

    $.validator.addMethod("lettersonly", function (value, element) {
        return this.optional(element) || /^[a-z]+$/i.test(value);
    }, "Letters only please");
    $.validator.addMethod('qq', function (v, element) {
        v = v.replace(/\s+/g, "");
        return this.optional(element) || /^[1-9][0-9]{4,10}$/i.test(v);

    }, '请输入正确的QQ号！');

    $.validator.addMethod("phone", function (v, element) {
        v = v.replace(/\s+/g, "");
        //(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)
        //兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)
        // (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
        // return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
        return this.optional(element) || /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(v);
    }, "请输入正确的电话号码！");
    $.validator.addMethod("mobile", function (v, element) {
        v = v.replace(/\s+/g, "");
        var tel = /(^0{0,1}13[0-9]{9}$)|(13\d{9}$)|(15[0135-9]\d{8}$)|(18[267]\d{8}$)/;
        return this.optional(element) || tel.exec(v);
    }, "请输入正确的手机号码！");
    $.validator.addMethod("phoneormobile", function (v, element) {
        v = v.replace(/\s+/g, "");
        var telReg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,5}))?$/;
        // /(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)|(13\d{9}$)|(15[0135-9]\d{8}$)|(18[267]\d{8}$)/ ;
        var mobileReg = /(^0{0,1}13[0-9]{9}$)|(13\d{9}$)|(15[0135-9]\d{8}$)|(18[267]\d{8}$)/;
        return this.optional(element) || telReg.test(v) || mobileReg.exec(v);
    }, "请输入正确的电话号码！");

    $.validator.addMethod("postcode", function (v, element) {
        v = v.replace(/\s+/g, "");
        return this.optional(element) || v.match(/^[0-9]{6}$/);
    }, "Please specify a valid postcode");

    $.validator.addMethod("date", function (v, element) {
        v = v.replace(/\s+/g, "");
        return this.optional(element) || v.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/);
    });
    $.validator.addMethod("password", function (v, element) {
        v = v.replace(/\s+/g, "");
        return this.optional(element) || /^(?=^.{6,12}$)(?=(?:.*?\d){2})(?=.*[a-z])(?=(?:.*?[A-Z]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/i.test(v);
    }, '请输入符合格式的密码！');

    $.validator.addClassRules({
        alphanumeric: { alphanumeric: true },
        lettersonly: { lettersonly: true },
        phone: { phone: true },
        postcode: { postcode: true }
    });
    //$.validator.unobtrusive.adapters.addBool("password").addBool("phone").addBool("mobile").addBool("phoneormobile");
})(jQuery);

